package com.moli.iov.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.base.cache.redis.CompanyVehicleModelCacheService;
import com.moli.iov.base.model.CompanyVehicleModelParam;
import com.moli.iov.base.model.dto.VehicleCommand;
import com.moli.iov.base.model.dto.req.CompanyVehicleModelReqDto;
import com.moli.iov.base.model.dto.req.QueryCompanyVehicleModelListParamDto;
import com.moli.iov.base.model.dto.res.CompanyVehicleModelResDto;
import com.moli.iov.base.model.po.CompanyVehicleModel;
import com.moli.iov.base.service.ICompanyVehicleModelService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.base.mapper.CompanyVehicleModelMapper;
import com.moli.iov.util.ValidationUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 公司车型类
 *
 * @author whl
 * @version 3.0
 */
@Slf4j
@Service()
@Transactional(rollbackFor = Exception.class)
public class ICompanyVehicleModelServiceImpl extends
    ServiceImpl<CompanyVehicleModelMapper, CompanyVehicleModel> implements
    ICompanyVehicleModelService {

  @Autowired
  private CompanyVehicleModelCacheService companyVehicleModelCacheService;


  @Override
  public RestResponse saveCompanyVehicleModel(CompanyVehicleModelReqDto companyVehicleModelReqDto) {
    ValidationUtils.validateBean(companyVehicleModelReqDto);
    CompanyVehicleModel companyVehicleModel = new CompanyVehicleModel();
    CommonUtils.copyProperties(companyVehicleModelReqDto, companyVehicleModel);
    CompanyVehicleModelParam companyVehicleModelParam = new CompanyVehicleModelParam();
    companyVehicleModelParam
        .queryEqCompanyId(companyVehicleModelReqDto.getCompanyId())
        .queryEqSupplierName(companyVehicleModelReqDto.getSupplierName())
        .queryEqCompanyVehicleModel(companyVehicleModelReqDto.getVehicleModel())
        .queryEqDeviceModel(companyVehicleModelReqDto.getDeviceModel());
    CompanyVehicleModel one = super.getOne(companyVehicleModelParam.get(), false);
    if (one != null) {
      return RestResponse.failure("操作失败！数据已存在");
    }
    boolean save = saveOrUpdate(companyVehicleModel);
    if (save) {
      companyVehicleModelCacheService.setCompanyVehicleModel(companyVehicleModel);
      return RestResponse.success();
    } else {
      return RestResponse.failure("操作失败！");
    }
  }


  @Override
  public RestResponse<PageExtend<CompanyVehicleModelResDto>> listCompanyVehicleModels(
      QueryCompanyVehicleModelListParamDto queryParam) {
    CompanyVehicleModelParam companyVehicleModelParam = new CompanyVehicleModelParam();
    companyVehicleModelParam
        .queryEqCompanyId(queryParam.getCompanyId())
        .queryEqDeviceModel(queryParam.getDeviceModel())
        .queryEqSupplierName(queryParam.getSupplierName())
         .queryInIdList(queryParam.getIdList())
        .gmtModifiedOrderByDesc();
    IPage<CompanyVehicleModel> query = new Page(queryParam.getPageNum(), queryParam.getPageSize());
    IPage<CompanyVehicleModel> page = page(query, companyVehicleModelParam.get());
    PageExtend<CompanyVehicleModelResDto> res = new PageExtend<>();
    if (!CollectionUtils.isEmpty(page.getRecords())) {
      List<CompanyVehicleModelResDto> dtos = new ArrayList<>();
      for (CompanyVehicleModel record : page.getRecords()) {
        CompanyVehicleModelResDto dto = new CompanyVehicleModelResDto();
        CommonUtils.copyProperties(record, dto);
        if (StringUtils.isNotBlank(record.getVehicleCommand())){
          dto.setVehicleCommandList(JSON.parseArray(record.getVehicleCommand(), VehicleCommand.class));
        }
        dtos.add(dto);
      }
      res.setRecords(dtos);
    }
    res.setPageSize(page.getSize());
    res.setTotal(page.getTotal());
    res.setPageNumber(page.getCurrent());
    return RestResponse.success(res);
  }


  @Override
  public RestResponse updateCompanyVehicleModel(
      CompanyVehicleModelReqDto dto) {
    ValidationUtils.validateBean(dto, CompanyVehicleModelReqDto.Update.class);
    CompanyVehicleModel companyVehicleModel = new CompanyVehicleModel();
    CommonUtils.copyProperties(dto, companyVehicleModel);
    CompanyVehicleModel old = super.getById(dto.getId());
    //淘汰缓存
    companyVehicleModelCacheService.deleteCompanyVehicleModel(old);
    boolean update = super.saveOrUpdate(companyVehicleModel);
    if (update) {
      companyVehicleModelCacheService.setCompanyVehicleModel(companyVehicleModel);
    }
    return update ? RestResponse.success() : RestResponse.failure("操作失败");
  }

  @Override
  public RestResponse<CompanyVehicleModelResDto> getCompanyVehicleModel(
      Long id) {
    ValidationUtils.validateBean(id);
    CompanyVehicleModel result = companyVehicleModelCacheService
        .getCompanyVehicleModelById(id);
    if (result != null) {
      CompanyVehicleModelResDto resDto = new CompanyVehicleModelResDto();
      CommonUtils.copyProperties(result, resDto);
      if (StringUtils.isNotBlank(result.getVehicleCommand())){
        resDto.setVehicleCommandList(JSON.parseArray(result.getVehicleCommand(), VehicleCommand.class));
      }
      return RestResponse.success(resDto);
    } else {
      CompanyVehicleModel companyVehicleModel = super.getById(id);
      if (companyVehicleModel != null) {
        companyVehicleModelCacheService.setCompanyVehicleModel(companyVehicleModel);
        CompanyVehicleModelResDto resDto = new CompanyVehicleModelResDto();
        CommonUtils.copyProperties(companyVehicleModel, resDto);
        if (StringUtils.isNotBlank(companyVehicleModel.getVehicleCommand())){
          resDto.setVehicleCommandList(JSON.parseArray(result.getVehicleCommand(), VehicleCommand.class));
        }
        return RestResponse.success(resDto);
      }
      return null;
    }
  }


  @Override
  public List<String> listVehicleCommand() {
    List<String> strings = companyVehicleModelCacheService.listVehicleCommand();
    if (strings != null && !strings.isEmpty()) {
      return strings;
    } else {
      CompanyVehicleModelParam companyVehicleModelParam = new CompanyVehicleModelParam();
      List<CompanyVehicleModel> list = this.list(companyVehicleModelParam.get());
      if (list != null && !list.isEmpty()) {
        for (CompanyVehicleModel companyVehicleModel : list) {
          //添加缓存
          companyVehicleModelCacheService.setCompanyVehicleModel(companyVehicleModel);
        }
        return list.stream().map(CompanyVehicleModel::getVehicleCommand)
            .collect(Collectors.toList());
      }
      return null;
    }
  }

  @Override
  public Map<String, String> listVehicleCommandMap() {
    Map<String, String> map = companyVehicleModelCacheService.listVehicleCommandMap();
    if (map != null && !map.isEmpty()) {
      return map;
    } else {
      CompanyVehicleModelParam companyVehicleModelParam = new CompanyVehicleModelParam();
      List<CompanyVehicleModel> list = this.list(companyVehicleModelParam.get());
      if (list != null && !list.isEmpty()) {
        companyVehicleModelCacheService.saveCompanyVehicleModels(list);
        return companyVehicleModelCacheService.listVehicleCommandMap();
      }
      return null;
    }
  }

  @Override
  public void initCompanyVehicleModel() {
    //清空缓存
    companyVehicleModelCacheService.initCompanyVehicleModel();
    CompanyVehicleModelParam modelParam = new CompanyVehicleModelParam();
    List<CompanyVehicleModel> list = list(modelParam.get());
    if (!CollectionUtils.isEmpty(list)) {
      for (CompanyVehicleModel model : list) {
        companyVehicleModelCacheService.setCompanyVehicleModel(model);
      }
    }
  }
}
